package com.s10.ecloud.web.action;

import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.s10.ecloud.web.Constants;
import com.s10.ecloud.web.model.IndividualUser;
import com.s10.ecloud.web.model.User;
import com.s10.ecloud.web.service.IUserService;

/**
 * 
 * 个人用户基本信息修改
 * 
 * @author Kevin
 * 
 * 
 */
public class A010101Action extends ActionSupport {

	@Resource
	private IUserService userService;

	@Action(value = "/toEditIndUserInfo", results = { @Result(name = "success", location = "/page/ind/editIndUserInfo.jsp") })
	public String toEditPasswordPage() throws Exception {

		IndividualUser currentUser = ((IndividualUser) ActionContext
				.getContext().getSession().get(Constants.SESSION_KEY_USER));

		this.setEmail(currentUser.getEmail());

		this.setMobile(currentUser.getMobile());
		this.setName(currentUser.getName());
		this.setPhone(currentUser.getPhone());
		this.setQq(currentUser.getQq());

		return this.SUCCESS;
	}

	@Action(value = "/updateIndUserInfo", results = {
			@Result(name = "success", location = "/page/ind/index.jsp"),
			@Result(name = "error", location = "/page/ind/editIndUserInfo.jsp") })
	public String updateEntUserInfo() throws Exception {

		// 1 注册信息验证
		if (!this.validateAddMember()) {
			return ERROR;
		}

		User currentUser = (User) ActionContext.getContext().getSession()
				.get(Constants.SESSION_KEY_USER);

		currentUser = userService.getUser(currentUser);

		currentUser.setMobile(this.getMobile());
		currentUser.setName(this.getName());

		currentUser.setPhone(this.getPhone());
		currentUser.setQq(this.getQq());

		java.sql.Date dateNow = new java.sql.Date(
				new java.util.Date().getTime());
		currentUser.setUpdateDate(dateNow);

		User u = userService.updateUser(currentUser);
		// 更新session中的内容
		ActionContext.getContext().getSession()
				.put(Constants.SESSION_KEY_USER, u);

		return this.SUCCESS;
	}

	/** 手动进行新增注册会员的表单验证 */
	private boolean validateAddMember() {

		/** 账户信息验证 */

		/** 联系人信息验证 */
		// 联系人姓名
		if (this.getName() == null || this.getName().trim().length() < 1) {
			addFieldError("name", getText("请输入联系人姓名"));
		}
		// 固定电话
		if (this.getPhone() != null
				&& this.getPhone().trim().length() > 0
				&& !Pattern.matches("^[0-9\\-()（）]{7,18}$", this.getPhone()
						.trim())) {
			addFieldError("phone", getText("member_validation_phone"));
		}
		// 手机
		if (this.getMobile() != null
				&& this.getMobile().trim().length() > 0
				&& !Pattern.matches("^0?(13|15|18|14)[0-9]{9}$", this
						.getMobile().trim())) {
			addFieldError("mobile", getText("member_validation_phone"));
		}

		// QQ
		if (this.getQq() == null || this.getQq().trim().length() < 1) {
			addFieldError("QQ", getText("请输入QQ号码"));
		}

		// 表单验证失败
		if (hasFieldErrors()) {
			return false;
		} else {
			return true;
		}
	}

	private String email;
	private String password;
	private String confirmPassword;

	private String name;
	private String phone;
	private String mobile;
	private String qq;

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the confirmPassword
	 */
	public String getConfirmPassword() {
		return confirmPassword;
	}

	/**
	 * @param confirmPassword
	 *            the confirmPassword to set
	 */
	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}

	/**
	 * @param phone
	 *            the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}

	/**
	 * @return the mobile
	 */
	public String getMobile() {
		return mobile;
	}

	/**
	 * @param mobile
	 *            the mobile to set
	 */
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	/**
	 * @return the qq
	 */
	public String getQq() {
		return qq;
	}

	/**
	 * @param qq
	 *            the qq to set
	 */
	public void setQq(String qq) {
		this.qq = qq;
	}
}
